package com.zbest.zpay.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.zbest.zpay.web.form.BaseForm;

/**
 */
@Component
@Aspect
public class LogAdvice {

    private static final Logger logger = LoggerFactory.getLogger(LogAdvice.class);

    /**
     * 前置通知
     *
     * @param jp
     */
    @Around("execution(public * com.zbest.zpay.web.controller..*.*(..))")
    public Object doBefore(ProceedingJoinPoint jp) throws Throwable {
        Object[] objectArray = jp.getArgs();

        if (null == objectArray || objectArray.length <= 0) {
            return jp.proceed();
        }
        String url = "";

        //获取请求来源和请求的url
        for (Object object : objectArray) {
            if (object instanceof HttpServletRequest) {
                HttpServletRequest httpServletRequest = (HttpServletRequest) object;
                url = httpServletRequest.getRequestURI();
            }
        }

        for (Object object : objectArray) {
            if (object instanceof BaseForm) {
                logger.info("请求的url=" + url + ",请求的表单是form=" + object);
            }
        }
        Object object = jp.proceed();
        logger.info("请求结果是" + object);
        return object;
    }

}
